package com.jueming.miser.user.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.jueming.miser.user.to.MiserUserTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jueming.miser.user.entity.MiserUserEntity;
import com.jueming.miser.user.service.MiserUserService;
import com.jueming.miser.common.utils.PageUtils;
import com.jueming.miser.common.utils.R;



/**
 * 
 *
 * @author jueming
 * @email jueming@qq.com
 * @date 2022-10-04 11:13:16
 */
@RestController
@RequestMapping("user/miseruser")
public class MiserUserController {
    @Autowired
    private MiserUserService miserUserService;

    @RequestMapping("/register")
    R register(@RequestBody MiserUserTo to) {
        MiserUserEntity entity = new MiserUserEntity();
        BeanUtils.copyProperties(to, entity);
        return save(entity);
    }

    @RequestMapping("/login")
    R login(@RequestBody MiserUserTo to) {
        Integer userId = to.getUserId();
        String userPassword = to.getUserPassword();
        MiserUserEntity entity = miserUserService.getById(userId);

        if (entity.getUserPassword().equals(userPassword)) {
            return R.ok().put("data", entity);
        } else {
            return R.error("登陆失败");
        }
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = miserUserService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    
    public R info(@PathVariable("userId") Integer userId){
		MiserUserEntity miserUser = miserUserService.getById(userId);

        return R.ok().put("miserUser", miserUser);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    
    public R save(@RequestBody MiserUserEntity miserUser){
		miserUserService.save(miserUser);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    
    public R update(@RequestBody MiserUserEntity miserUser){
		miserUserService.updateById(miserUser);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    
    public R delete(@RequestBody Integer[] userIds){
		miserUserService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
